package advanced.filter;

import java.util.ArrayList;

import JaCoP.constraints.XplusYeqZ;
import JaCoP.core.IntVar;
import JaCoP.core.Store;

public class Operation {
	private String type;
	private int id;
	private Store store;
	private IntVar origin, end, duration;
	private boolean isOut;
	private ArrayList<Operation> dependencies;
	
	public Operation(Store store, String type, int id, int duration, int maxValue) {
		this.type = type;
		this.id = id;
		this.store = store;
		this.duration = new IntVar(this.store, type + id + "origin", duration, duration);
		this.store = store;
		isOut = true;
		origin = new IntVar(this.store, type + id + "origin", 0, maxValue);
		end = new IntVar(store, type + id + "end", 0, maxValue);
		this.store.impose(new XplusYeqZ(origin, this.duration, end));
		dependencies = new ArrayList<Operation>();
	}
	
	public void addDependency(Operation x) {
		dependencies.add(x);
	}
	
	public ArrayList<Operation> getDependencies() {
		return dependencies;
	}
	
	public IntVar getOrigin() {
		return origin;
	}
	
	public IntVar getDuration() {
		return duration;
	}
	
	public IntVar getEnd() {
		return end;
	}
	
	public String getType() {
		return type;
	}
	
	public void setDependent() {
		isOut = false;
	}
	
	public boolean isOut() {
		return isOut;
	}

	
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Operation other = (Operation) obj;
		if (id != other.id)
			return false;
		if (type == null) {
			if (other.type != null)
				return false;
		} else if (!type.equals(other.type))
			return false;
		return true;
	}

	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("Type: ").append(type).append(", ");
		sb.append("ID: ").append(id).append(", ");
		sb.append("Origin: ").append(origin.value()).append(", ");
		sb.append("Duration : ").append(duration).append(", ");
		sb.append("End: ").append(end.value()).append(", ");
		sb.append("Is out: ").append(isOut).append(", ");
		sb.append("Dependencies: ");
		for(Operation o : dependencies) {
			sb.append(o.shortString()).append(", ");
		}
		
		return sb.toString();
	}
	
	public String shortString() {
		return type + id;
	}
	
	
	
	
	

}
